package chu.dejian.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sam.blemodule.R;

/**
 * Created by Administrator on 2015/12/12 0012.
 */
public class AttentionLevelView extends TextView{

    public enum ATTENTION_LEVEL {
        NOTIFY_NOTHING(0),NOTIFY_EMERGENT(1),NOTIFY_WARNING(2),NOTIFY_ALL(3);
        int value = -1;
        ATTENTION_LEVEL(int value){
            this.value = value;
        }
        public static ATTENTION_LEVEL fromInt(int value){
            switch (value){
                case 0: return NOTIFY_NOTHING;
                case 1: return NOTIFY_EMERGENT;
                case 2: return NOTIFY_WARNING;
                case 3: return NOTIFY_ALL;
                default:return null;
            }
        }
        public int value(){
            return value;
        }
    }
    public ATTENTION_LEVEL attentionLevel = null;

    private boolean editable = true;  // 默认可编辑
    private int levelValue = -1; //默认显示标题：关注级别
    private boolean clickable = true;
    AttentionLevelMenu levelMenu;
    private Context context;

    public AttentionLevelView(Context context) {
        this(context,null);
    }

    public AttentionLevelView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AttentionLevelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttrs(context, attrs);
        init();
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.AttentionLevelView);
        editable = ta.getBoolean(R.styleable.AttentionLevelView_editable,editable);
        levelValue = ta.getInt(R.styleable.AttentionLevelView_level, levelValue);
        ta.recycle();
    }

    protected void init(){
        setLevelByValue(levelValue);
        setEditable(editable);
    }

    public void setEditable(boolean editable){
        if(editable){
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(levelMenu == null) levelMenu = new AttentionLevelMenu(context,AttentionLevelView.this);
                    if(levelMenu.isShowing()) {
                        levelMenu.dismiss();
                        if(spinnerCallBack != null) spinnerCallBack.onSpinnerClose();
                    } else {
                        levelMenu.showAsDropDown(AttentionLevelView.this);
                        if(spinnerCallBack != null) spinnerCallBack.onSpinnerOpen();
                    }
                }
            });
        } //else setOnClickListener(null);
    }

    public void setLevelByValue(int levelValue){
        setLevel(ATTENTION_LEVEL.fromInt(levelValue));
    }

    public void setLevel(ATTENTION_LEVEL level){
        attentionLevel = level;
        if(level == ATTENTION_LEVEL.NOTIFY_ALL){
            setText(R.string.notify_all);
            setTextColor(getResources().getColor(R.color.blue_text_color));
            setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.color_block_5dbef5), null, null, null);
        } else if(level == ATTENTION_LEVEL.NOTIFY_WARNING){
            setText(R.string.notify_warning);
            setTextColor(getResources().getColor(R.color.green_text_color));
           setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.color_block_7ed321), null, null, null);
        } else if(level == ATTENTION_LEVEL.NOTIFY_EMERGENT){
            setText(R.string.notify_emergent);
            setTextColor(getResources().getColor(R.color.orange_text_color));
            setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.color_block_f5a623), null, null, null);
        } else if(level == ATTENTION_LEVEL.NOTIFY_NOTHING){
            setText(R.string.notify_nothing);
            setTextColor(getResources().getColor(R.color.gray_text_color));
            setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.color_block_929292), null, null, null);
        } else if(level == null){
            setText(R.string.attention_level);
            setTextColor(getResources().getColor(R.color.gray_text_color));
            setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.color_block_c8c7cc), null, null, null);
        }
    }

    public ATTENTION_LEVEL getAttentionLevel(){
        return attentionLevel;
    }

    public int getAttentionLevelValue(){
        return attentionLevel.ordinal();
    }

    private SpinnerCallBack spinnerCallBack;
    public void setSpinnerCallBack(SpinnerCallBack spinnerCallBack){
        this.spinnerCallBack = spinnerCallBack;
    }
    public interface SpinnerCallBack{
        void onSpinnerOpen();
        void onSpinnerClose();
    }


    //========================================================================================

    public class AttentionLevelMenu extends PopupWindow {
        private View rootView,parent;
        private View notifyAll,notifyWarning,notifyEmergent,notifyNoting;

        public AttentionLevelMenu(Context mContext, View parent) {
            this.parent = parent;

            rootView = View.inflate(mContext, R.layout.dialog_attention_level_menu, null);

            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(rootView);
            update();

            findView();
            initView();
        }

        public void show() {
            showAtLocation(parent, Gravity.BOTTOM, 0, -rootView.getHeight());
        }

        private void findView() {
            notifyAll = rootView.findViewById(R.id.notify_all);
            notifyWarning = rootView.findViewById(R.id.notify_warning);
            notifyEmergent = rootView.findViewById(R.id.notify_emergent);
            notifyNoting = rootView.findViewById(R.id.notify_noting);
        }

        private void initView() {
            notifyAll.setOnClickListener(onClickListener);
            notifyWarning.setOnClickListener(onClickListener);
            notifyEmergent.setOnClickListener(onClickListener);
            notifyNoting.setOnClickListener(onClickListener);
            if(attentionLevel == null) return;
            if(attentionLevel.ordinal() < ATTENTION_LEVEL.NOTIFY_ALL.ordinal()) notifyAll.setVisibility(GONE);
            if(attentionLevel.ordinal() < ATTENTION_LEVEL.NOTIFY_WARNING.ordinal()) notifyWarning.setVisibility(GONE);
            if(attentionLevel.ordinal() < ATTENTION_LEVEL.NOTIFY_EMERGENT.ordinal()) notifyEmergent.setVisibility(GONE);
        }

        private OnClickListener onClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                int i = v.getId();
                if (i == R.id.notify_all) {
                    AttentionLevelView.this.setLevel(ATTENTION_LEVEL.NOTIFY_ALL);
                    if (spinnerCallBack != null) spinnerCallBack.onSpinnerClose();
                    dismiss();

                } else if (i == R.id.notify_warning) {
                    AttentionLevelView.this.setLevel(ATTENTION_LEVEL.NOTIFY_WARNING);
                    if (spinnerCallBack != null) spinnerCallBack.onSpinnerClose();
                    dismiss();

                } else if (i == R.id.notify_emergent) {
                    AttentionLevelView.this.setLevel(ATTENTION_LEVEL.NOTIFY_EMERGENT);
                    if (spinnerCallBack != null) spinnerCallBack.onSpinnerClose();
                    dismiss();

                } else if (i == R.id.notify_noting) {
                    AttentionLevelView.this.setLevel(ATTENTION_LEVEL.NOTIFY_NOTHING);
                    if (spinnerCallBack != null) spinnerCallBack.onSpinnerClose();
                    dismiss();

                } else {
                }

            }
        };
    }
}
